package com.fs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.pojo.House;
import com.fs.pojo.User;
import com.fs.service.HouseService;
import com.fs.service.SelectService;
import com.fs.service.UserService;
import com.fs.vo.Condition;
import com.fs.vo.ResponseData;
import com.fs.vo.ResultEnums;
import com.fs.vo.Search;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yyds
 * @fileName MyController
 * @date 2024/9/10 16:08
 * @description 控制器用于与客户端交互
 第一次修改提交
 **/
@RestController
@RequestMapping
public class MyController {
    @Resource
    private HouseService houseService;
    @Resource
    private UserService userService;
    @Resource
    private SelectService selectService;

    /**
     * 分页查询
     * @param condition
     * @return
     */

    @PostMapping("/findAll")
    public ResponseData<Page<House>> findAllPage(@RequestBody Condition condition) {
        return new ResponseData<>(ResultEnums.OK, houseService.findAllPage(condition));
    }

    @PostMapping("/findHid")
    public ResponseData<House> findHid(Integer id) {
        return new ResponseData<>(ResultEnums.OK, houseService.findById(id));
    }

    @PostMapping("/login")
    public ResponseData<User> login( User user) {
        return userService.login(user);
    }

    @PutMapping("/register")
    public ResponseData register(@RequestBody User user) {
        if (userService.register(user)) {
            return new ResponseData<>(ResultEnums.CREATED);
        } else
            return new ResponseData(ResultEnums.FAIL);
    }

    @PutMapping("/addHouse")
    public ResponseData addHouse(@RequestBody House house) {
        if (houseService.addHouse(house)) {
            return new ResponseData<>(ResultEnums.CREATED);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

    @PostMapping("/select")
    public List<ResponseData> select() {
        List<ResponseData> list = new ArrayList<>();
        list.add(selectService.AllHType());
        list.add(selectService.AllDistricts());
        list.add(selectService.AllStreets());
        return list;
    }

    @PostMapping("/myHouse")
    public ResponseData<List<House>> myHouse(Integer pid) {
        return houseService.myHouse(pid);
    }

    @DeleteMapping("/deleteHouse")
    public ResponseData deleteHouse(Integer hid) {
        return houseService.deleteHouse(hid) ? new ResponseData<>(ResultEnums.DELETED) : new ResponseData<>(ResultEnums.FAIL);
    }

    @PostMapping("/updateHouse")
    public ResponseData updateHouse(@RequestBody House house) {
        return houseService.updateHouse(house) ? new ResponseData<>(ResultEnums.UPDATED) : new ResponseData<>(ResultEnums.FAIL);
    }

    @PostMapping("/search")
    public ResponseData<Page<House>> search(@RequestBody Search search) {
        return new ResponseData<>(ResultEnums.OK,houseService.search(search));
    }

}
